package com.baijia.authentication.principal.populator;

import com.baijia.authentication.Authentication;
import com.baijia.authentication.AuthenticationMetaDataPopulator;
import com.baijia.authentication.principal.Credentials;
import com.baijia.authentication.principal.credentials.JigouCascadeAccountCredentials;
import com.baijia.dao.OrgAccountDao;
import com.baijia.dao.OrgCascadeAccountDao;
import com.baijia.dao.po.OrgAccount;
import com.baijia.dao.po.OrgCascadeAccount;
import com.baijia.util.JacksonUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @title JigouCascadeAccountMetaDataPopulator
 * @desc 补充authentication信息
 * @author caoliang
 * @date 2015年12月9日
 * @version 1.0
 */
@Component
public final class JigouCascadeAccountMetaDataPopulator implements AuthenticationMetaDataPopulator{
    
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    
    private static final String ORG_CASCADE_ACCOUNTID = "orgCascadeAccountId";
    private static final String ORG_CASCADE_ACCOUNT = "orgCascadeAccount";
    private static final String ORG_ACCOUNT = "orgAccount";
    
    @Autowired
    private OrgCascadeAccountDao orgCascadeAccountDao;
    @Autowired
    private OrgAccountDao orgAccountDao;
    
	@Override
	public Authentication populateAttributes(Authentication authentication,
			Credentials credentials) {
	    
	    JigouCascadeAccountCredentials jigouCredentials = (JigouCascadeAccountCredentials) credentials;
        String username = jigouCredentials.getUsername();
        
        logger.debug("Try to populate jigouCascadeAccount username:{}", username);
        OrgCascadeAccount orgCascadeAccount = orgCascadeAccountDao.findByUsername(username);
        OrgAccount orgAccount = orgAccountDao.getById(orgCascadeAccount.getOrgId());
        authentication.getAttributes().put(ACCOUNT_ID, orgAccount.getId());
        authentication.getAttributes().put(ACCOUNT_NUMBER, orgAccount.getNumber());
        authentication.getAttributes().put(ORG_CASCADE_ACCOUNTID, orgCascadeAccount.getId());
        try {
            orgAccount.setPassword(null);
            orgCascadeAccount.setPassword(null);
            authentication.getAttributes().put(ORG_ACCOUNT, JacksonUtil.obj2Str(orgAccount));
            authentication.getAttributes().put(ORG_CASCADE_ACCOUNT, JacksonUtil.obj2Str(orgCascadeAccount));
        } catch (IOException e) {
            logger.error("save orgCascadeAccount into dession error!", e);
        }
        
	    return authentication;
	}

	public void setOrgCascadeAccountDaoo(OrgCascadeAccountDao orgCascadeAccountDao) {
        this.orgCascadeAccountDao = orgCascadeAccountDao;
    }

    public void setOrgAccountDao(OrgAccountDao orgAccountDao) {
    
        this.orgAccountDao = orgAccountDao;
    }

    @Override
    public boolean supports(Credentials credentials) {
        return credentials instanceof JigouCascadeAccountCredentials;
            
    }
}
